/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package riteservices.model.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import riteservices.model.bean.Round;
import riteservices.model.bean.User;

/**
 *
 * @author sixviv
 */
public class UserDAO extends GeneralDAO<User>{

    
    public User getUserById(int id){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        criteria.setFetchMode("rounds", FetchMode.JOIN);
        criteria.setFetchMode("rounds.route", FetchMode.JOIN);
        criteria.setFetchMode("rounds_1", FetchMode.JOIN);
        criteria.setFetchMode("rounds_1.route", FetchMode.JOIN);
        criteria.setFetchMode("rounds_1.ratings", FetchMode.JOIN);
        criteria.setFetchMode("fbfriendsesForUserIdUser", FetchMode.JOIN);
        criteria.setFetchMode("fbfriendsesForUserIdUserF", FetchMode.JOIN);
        criteria.add(Restrictions.sqlRestriction("idUser="+id+""));
        User user = (User)criteria.uniqueResult();
        session.close();
        return user;
    }

    public boolean emailExist(String email){
        boolean exist=false;
        List<User> users= search("email='"+email+"'",User.class);
        if(!users.isEmpty())
            exist=true;
    return exist;
    }

    public User getUserData(String email){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        criteria.setFetchMode("rounds", FetchMode.JOIN);
        criteria.setFetchMode("rounds.route", FetchMode.JOIN);
        criteria.setFetchMode("rounds_1", FetchMode.JOIN);
        criteria.setFetchMode("rounds_1.route", FetchMode.JOIN);
        criteria.setFetchMode("fbfriendsesForUserIdUserF", FetchMode.JOIN);
        criteria.add(Restrictions.sqlRestriction("email='"+email+"'"));
        User user = (User)criteria.uniqueResult();
        session.close();
        return user;
    }

    public User getByFBId(String fbID){
        List<User> users = search("facebookid='"+fbID+"'",User.class);
        if(!users.isEmpty()){
            return users.get(0);
        }else{
            return null;
        }
    }



}
